package com.woniuxy.classdeans.controller;

import com.woniuxy.classdeans.exception.ClassdeansException;
import com.woniuxy.classdeans.service.TeamInfoService;
import com.woniuxy.classdeans.service.TeamInstructorService;
import com.woniuxy.classdeans.service.TeamMasterService;
import com.woniuxy.commons.entity.ResponseResult;
import com.woniuxy.commons.entity.wyh.ResponseTable;
import com.woniuxy.commons.entity.wyh.TeamCondition;
import com.woniuxy.commons.entity.wyh.TeamInfo;
import com.woniuxy.commons.entity.wyh.TeamInstructor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级信息
 *
 * @author 王艳辉
 * @date 2020/7/17 - 12:04
 */

@RestController
@RequestMapping("teamInfo")
@Transactional
@Slf4j
@Api(tags = "Teaminfo管理接口")
public class TeamInfoController {
    @Autowired
    private TeamInfoService teamInfoService;

    //班级任课老师
    @Autowired
    private TeamInstructorService teamInstructorService;

    //班主任
    @Autowired
    private TeamMasterService teamMasterService;
    /**
     * 查询所有班级信息
     *
     * @param page  页码
     * @param limit 页码大小
     * @return
     */
    @ApiOperation("获取所有老师")
    @RequestMapping("getall")
    public ResponseTable<TeamInfo> getall(@ApiParam(name = "page",value = "页码") Integer page, Integer limit) {
        //System.out.println(page+"-----------------"+limit);
        //默认返回失败
        ResponseTable<TeamInfo> result = new ResponseTable<TeamInfo>(400, "获取数据失败");
        try {
            List<TeamInfo> all = teamInfoService.getall(page, limit);//获取所有班级信息

            //获取记录总条数
            int total = (int) teamInfoService.getTotal();

            if (result != null) {
                result.setCode(0).setCount(total).setMsg("获取成功").setData(all);
            }
            System.out.println("data:" + result.getData());
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("teamInfo/getall异常");
        }

        return result;
    }

    /**
     * 根据条件查询班级
     *
     * @param condition
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("getByCondition")
    public ResponseTable<TeamInfo> getByCondition(TeamCondition condition, Integer page, Integer limit) {
//        System.out.println("-----------条件查询-----------");
//        System.out.println(page);
//        System.out.println(condition);
//        System.out.println(limit);
        //默认返回失败
        ResponseTable<TeamInfo> result = new ResponseTable<TeamInfo>(400, "获取数据失败");
        try {
            //更加条件查询所有信息
            List<TeamInfo> res = teamInfoService.getByCondition(condition, page, limit);

            int count = (int) teamInfoService.getCountByCondition(condition);

            if (res != null) {
                result.setCode(0).setCount(10).setMsg("获取成功").setData(res);
            }
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("teamInfo/getByCondition异常");
        }

        //System.out.println("查询结果为：" + res);


        return result;
    }

    //根据id获取
    @RequestMapping("getByTeid")
    public TeamInfo getByTeid(TeamInfo teamInfo) {
        return teamInfoService.getByTeid(teamInfo);
    }


    //根据id更新班级信息
    @RequestMapping("updateByTeid")
    public int updateByTeid(TeamInfo teamInfo) {
        return teamInfoService.updateByTeid(teamInfo);
    }

    /**
     * 新增班级信息
     *
     * @param teamInfo
     * @return
     */
    @PostMapping("add")
    @Transactional//开启事务管理
    public ResponseResult add(TeamInfo teamInfo) {

        //设置初始人数为0
        teamInfo.setTnumber(0);
        //默认flag=1正常
        teamInfo.setFlag(1);

        System.out.println("新增的对象为：" + teamInfo);
        //返回消息类型
        ResponseResult res = new ResponseResult().setCode(400).setMessage("失败！");

        try {
            //新增班级信息
            int num = teamInfoService.add(teamInfo);
            //新增班主任信息

            if (num == 1) {
                //新增成功后获得班级id，新增班级任课老师信息
                num= teamInstructorService.addTeamInstructor(new TeamInstructor().setTeId(teamInfo.getTeId()));
                if (num == 1)res.setCode(200).setMessage("新增班级成功");
            }
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("teamInfo/add异常");
        }


        return res;
    }

    /**
     * 更新班级信息表
     *
     * @param teamInfo
     * @return data中返回最新的信息前台渲染
     */
    @RequestMapping("updateTeamInfo")
    public ResponseResult<TeamInfo> updateGtadeInfo(TeamInfo teamInfo) {



        //返回消息类型
        ResponseResult<TeamInfo> res = new ResponseResult<TeamInfo>().setCode(400).setMessage("失败！");


        if(teamInfo==null)
            return  res;
        try {
            TeamInfo predata = teamInfoService.getByTeid(teamInfo);

            //如果修改过了班主任
            if (teamInfo.getTmId()!=0&&predata.getTmId()!=teamInfo.getTmId()){
                //逻辑删除原先的班主任
                teamMasterService.delByTmId(predata.getTmId());
            }


            //System.out.println("teamInfo为：" + teamInfo);
            //更新班级信息
            int num = teamInfoService.updateByTeid(teamInfo);

            //如果更新成功
            if (num == 1) {
                //获取更新后的信息
                teamInfo = predata;
                //返回前端页面
                res.setCode(200).setMessage("成功！").setData(teamInfo);

            }
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("teamInfo/updateTeamInfo异常");
        }


        return res;
    }


    /**
     * @param checkedArr 获取前端数组
     * @return
     */
    @RequestMapping(value = "/delBatch", method = {RequestMethod.POST})
    public ResponseResult delBatch(@RequestBody String[] checkedArr) {

        //System.out.println(checkedArr.length);

        //返回消息类型默认失败
        ResponseResult<TeamInfo> res = new ResponseResult<TeamInfo>().setCode(400).setMessage("失败！");

        try {
            int num = teamInfoService.delBatch(checkedArr);
            //删除成功
            if (num > 0) {
                //返回前端页面
                res.setCode(200).setMessage("成功！");
            }
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("teamInfo/delBatch异常");
        }


        return res;
    }

    /**
     * 根据id删除班级信息
     * @param teamInfo
     * @return
     */
    @RequestMapping("delByTeid")
    public ResponseResult delByTeid(TeamInfo teamInfo) {

        //System.out.println("teid------------------"+teamInfo.getTeId());
        //返回消息类型默认失败
        ResponseResult<TeamInfo> res = new ResponseResult<TeamInfo>().setCode(400).setMessage("删除失败！");
        try {
            int num = teamInfoService.delByTeid(teamInfo);
            //删除成功
            if (num > 0) {
                //返回前端页面
                res.setCode(200).setMessage("删除成功！");
            }
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("teamInfo/delByTeid异常");
        }


        return res;
    }
}
